package com.hashnode.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.hashnode.service.TestSentinelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
public class FlowLimitController {

    @Autowired
    TestSentinelService testSentinelService;

    @GetMapping("/testA")
    public String testA()
    {

        return "------testA";
    }

    @GetMapping("/testB")
    public String testB()
    {
        return "------testB";
    }

    @GetMapping("/testC/common")
    public String testC()
    {
        return testSentinelService.testSentinelService();
//        return "------testC";
    }

    @GetMapping("/testD/common")
    public String testD()
    {
        return testSentinelService.testSentinelService();
//        try{
//            TimeUnit.SECONDS.sleep(1);
//        }catch (InterruptedException e){
//            e.printStackTrace();
//        }
//        return "------testD";
    }

    @GetMapping("/testE")
    public String testE()
    {
        try{
            TimeUnit.SECONDS.sleep(1);
        }catch (InterruptedException e){
            e.printStackTrace();
        }
        return "------testE";
    }

    @GetMapping("/testF")
    public String testF(Integer id)
    {
        if(id != null && id < 0){
            throw new RuntimeException("运行异常");
        }
        return "------testF";
    }

    @GetMapping("/testHotkey")
    @SentinelResource(value = "testHotkey",blockHandler = "deal_testHotkey")
    public String testHotKey(@RequestParam(value = "p1",required = false)String p1,
                             @RequestParam(value = "p2",required = false) String p2){
        return "----------testHotkey";
    }

    public String deal_testHotkey(String p1, String p2, BlockException exception){
        return "------deal_testHotkey";
    }
}
